################################
# Definition des variables
################################
PATCHD=./Patches/
PROJECTD=./Project/
LASTD=./Last/
DATE=`date "+%Y:%m:%d"`
PATCHFILE=projet-\:\:date\:`date "+%Y:%m:%d"`\:\:-\:\:$(COMMIT)\:\:.patch
PATCHFILE_R=$(wildcard $(PATCHD)*:`expr $(COMMIT) - 1`::.patch)
#\:\:`expr $(COMMIT) - 1`\:\:.patch)
#PATCHFILE_R=\:\:`expr $(COMMIT) - 1`\:\:.patch
################################
# Inclusion des infos de version
################################
-include Project/version

################################
# Regles de production
################################

all : build

info:
	@echo "project version: $(MAJOR).$(MINOR).$(BUILD)"
	@echo "project commits: $(COMMIT)"

#
# commit
# increment du major
#
major: commit
	@sed  -i "s/^MAJOR=[0-9]*/MAJOR=`expr $(MAJOR) + 1`/" $(PROJECTD)version
#@echo -e "MAJOR=`expr $(MAJOR) + 1`\nMINOR=$(MINOR)\nBUILD=$(BUILD)\nCOMMIT=$(COMMIT)" > $(PROJECTD)version

#
# commit
# increment du minor
#
minor:  commit
	@sed  -i "s/^MINOR=[0-9]*/MINOR=`expr $(MINOR) + 1`/" $(PROJECTD)version

#
# compile tout le projet
# si succes -> commit
#              increment du build
#
build:
	@cd $(PROJECTD); make all
	@if test -f $(PROJECTD)compilateur; then \
		sed  -i "s/^BUILD=[0-9]*/BUILD=`expr $(BUILD) + 1`/" $(PROJECTD)version ;\
	else \
		cd $(PROJECTD); make distclean; \
	fi
#
# creation des patches dans Patches
# sauvegarde du projet dans Last
# increment du commit
#
commit:
	-diff  $(PROJECTD) $(LASTD) > $(PATCHD)$(PATCHFILE)
	@rm -r $(LASTD)*
	@cp -rfu $(PROJECTD)* $(LASTD)
	@sed  -i "s/^COMMIT=[0-9]*/COMMIT=`expr $(COMMIT) + 1`/" $(PROJECTD)version

revert:
	@rm -r $(PROJECTD)*
	@cp -rfu $(LASTD)* $(PROJECTD)

backup:  revert
	i=$(COMMIT) ; \
	while [[ $$i -ge $(CMT) ]] ; do \
		find $(PROJECTD) -type f -exec patch -p0 -f -i  $(PATCHD)projet-*-::$$i::.patch {} \; ;\
		((i = i - 1)) ; \
	done
	@find ./ -iname '*.rej' -delete
	@find ./ -iname '*.orig' -delete 



#@find $(PROJECTD) -type f -exec patch -p0 -f -i  $(PATCHD)projet-*-::$(CMT)::.patch {} \;
#-patch -p0 -i $(PATCHD)projet-*-::$(CMT)::.patch

.PHONY: info project major minor build commit revert
